######################
#	lab 02
#	------
#	@author:Conrad Stack
######################

### Part 1

# set up function logistic()
# K = carrying capacity 
# r = intrinsic growth rate
# x0 = initial population size
logist = function(r, K, length = 200, x0=70)
{
	x = rep(NA, length)  # vector to hold pop data from each simulation (1D) 
	
	x[1] = x0 #setting initial abundance (x0 = N0) 
	
	#use logistic model to fill x
	for(i in 2:length)
	{
		x[i] = x[i-1]*exp(r*(1-x[i-1]/K))  #Density Dependence
	}
	
	return(x)
}

# QUANTILE-QUANTILE
# Simulate x0=70, K=100, r={1,2,3,4}
# store in matrix X
r=c(1,2,3,4)
K=100
length = 200
X = matrix(NA, nrow=length, ncol=length(r))  # set up a 100x4 matrice

for(i in 1:length(r))
{
	X[,i] = logist(r=r[i], K=100,length=length, x0=70)
} 


#setting up a graphics panel with 2 rows 
#and 2 cols (ie 4 plots)
par(mfrow=c(2,2)) 

#for each r, plot simulation (pop vs. time)
plot(X[,1], xlab="time", ylab="abundance") 
title("r=1")
plot(X[,2], xlab="time", ylab="abundance")
title("r=2")
plot(X[,3], xlab="time", ylab="abundance")
title("r=3")
plot(X[,4], xlab="time", ylab="abundance")
title("r=4")
#ALT
#plot(X[,1], xlab="time", ylab="abundance")
#points(X[,2],col="red")
#points(X[,3],col="blue") 
#points(X[,4],col="cyan") 




#PHASE PLANE
#creating the state space plot with Xt as a function of 
#Xt-1. First line is theoretical curve between 0 and K
curve(x*exp(1*(1-x/100)),0,200, xlab="Xt-1", ylab="Xt")
#adding the 1-to-1 line
lines(c(0,250), c(0,250))
points(X[1:199,1], X[2:200,1], col="red")
lines(X[1:199,1], X[2:200,1], col="red")



### Part 2